﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using FMPCore.Interfaces;

namespace Client
{
    /// <summary>
    /// Interaction logic for winLogin.xaml
    /// </summary>
    public partial class winLogin : Window
    {
        private IMessage m_msgProxy;

        public winLogin(IMessage msgProxy)
        {
            m_msgProxy = msgProxy;
            InitializeComponent();
        }

        /// <summary>
        /// Get/Set the Username
        /// </summary>
        public string Username
        {
            get
            {
                return txtUsername.Text;
            }
            set
            {
                txtUsername.Text = value;
            }
        }

        /// <summary>
        /// Get/Set the Password
        /// </summary>
        public string Password
        {
            get
            {
                return passboxPassword.Password;
            }
            set
            {
                passboxPassword.Password = value;
            }
        }

        private void btnConnect_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Login();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void Login()
        {
            bool result = m_msgProxy.Login(txtUsername.Text, passboxPassword.Password);

            this.DialogResult = result;
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void btnSignup_Click(object sender, RoutedEventArgs e)
        {
            winSignup signup = new winSignup(m_msgProxy);
            signup.Owner = this;
            Nullable<bool> dialogResult = signup.ShowDialog();
            if (dialogResult != null && dialogResult != false)
            {
                Username = signup.Username;
                Password = signup.Password;
                Login();
            }
        }
    }
}
